﻿using System;
using System.Collections.Generic;
using System.Text;
using ebay.Dal.CenterDB_OrderTableAdapters;
using System.Data.SqlClient;
using System.Data;

namespace ebay.Dal
{
    public class CustomerResolve
    {
        public void AddList(List<ebay.Model.CustomerResolve> list,string ebayUserId,string OrderId)
        {
            string cmdText = "Update Orders Set Standard = '',Mtime = getdate() Where ebayUserId = @EbayUserId And OrderId = @OrderId And Standard = '不完全代码'";
            string cmdText3 = "Update Orders Set IsOutStock = 'Y',Mtime = getdate() Where ebayUserId = @EbayUserId And OrderLineItemId = @OrderLineItemId";
            SqlParameter[] p = { 
                               new SqlParameter("@EbayUserId",SqlDbType.VarChar),
                               new SqlParameter("@OrderId",SqlDbType.VarChar)
                               };
            p[0].Value = ebayUserId;
            p[1].Value = OrderId;
            SqlParameter[] p2 = { 
                               new SqlParameter("@EbayUserId",SqlDbType.VarChar),
                               new SqlParameter("@OrderId",SqlDbType.VarChar)
                               };
            p2[0].Value = ebayUserId;
            p2[1].Value = OrderId;
            SqlParameter[] p3 = { 
                               new SqlParameter("@EbayUserId",SqlDbType.VarChar),
                               new SqlParameter("@OrderLineItemId",SqlDbType.VarChar)
                               };
            using (SqlConnection connection = new SqlConnection(SqlHelper.ceterDB_Conn))
            {
                connection.Open();
                T_CustomerResolveTableAdapter customAdapter = new T_CustomerResolveTableAdapter();
                customAdapter.Connection = connection;
                using (SqlTransaction trans = connection.BeginTransaction())
                {
                    customAdapter.Transaction = trans;
                    foreach (ebay.Model.CustomerResolve m in list)
                    {
                        int intVal = customAdapter.ScalarProduct(m.OrderLineItemId, m.EbayUserId, m.ProductId).Value;
                        if (intVal > 0)
                        {
                            customAdapter.UpdateQuantity(m.Quantity, m.EbayUserId, m.OrderLineItemId, m.ProductId);
                        }
                        else
                        {
                            customAdapter.Insert(m.EbayUserId, m.OrderLineItemId, m.ProductId, m.CustomerLable, m.Quantity, m.SalePrice, m.ItemCost, m.TransactionFees, m.PayFee,
                                m.TheoryPostAge, m.ActualPostage, m.PackageFee, m.RefundLoss, m.Rate, m.WarehouseId, m.MNote, m.Muser, m.Mtime, m.Auser, m.Atime);
                        }
                        p3[0].Value = ebayUserId;
                        p3[1].Value = m.OrderLineItemId;
                        SqlHelper.ExecuteNonQuery(trans, CommandType.Text, cmdText3, p3);
                    }
                    SqlHelper.ExecuteNonQuery(trans, CommandType.Text, cmdText, p);
                    try
                    {
                        trans.Commit();
                    }
                    catch {
                        trans.Rollback();
                    }
                }
            }

        }

    }
}
